<?php

namespace App\Admin\Controllers\posts;

use App\Common\statement\CommonStatement;
use App\Models\posts\Posts;
use App\Services\posts\PostsService;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class PostsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '帖子';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Posts);

        $grid->model()->latest();

        $grid->disableCreateButton();

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {

                $filter->where(function ($query) {

                    $query->whereHas('user',function ($query){
                        $query->where('uuid', 'like', "%{$this->input}%");
                    })
                        ->orWhere('id', 'like', "%{$this->input}%");

                },__('Uuid'));
                $filter->like('title',__('Title'));

            });

            $filter->column(1/3, function ($filter) {

                $filter->where(function ($query) {
                    $query->whereHas('user',function ($query){
                        $query->where('phone', 'like', "%{$this->input}%")
                            ->orWhere('name', 'like', "%{$this->input}%");
                    });

                },'昵称手机');

                $filter->equal('posts_cate_id',__('Posts cate id'))->select(PostsService::getCate());
            });


        });

        $grid->column('id', __('Id'));
        $grid->column('user_id', __('User id'));
        $grid->column('user.header_img', __('header_img'))
            ->lightbox(['width' => 50, 'height' => 50, 'class' => ['circle', 'thumbnail']]);
        $grid->column('type', __('Type'))->display(function ($s){
            return CommonStatement::posts_type[$s];
        });
        $grid->column('title', __('Title'));
        $grid->column('content', __('Content'));
        $grid->column('posts_cate_id', __('Posts cate id'));
        $grid->column('posts_cate.name', __('Posts cate name'));
        $grid->column('share_num', __('Share num'));
        $grid->column('up_num', __('Up num'));
        $grid->column('comment_num', __('Comment num'));
        $grid->column('goods_id', __('Goods id'));

        $grid->column('imgs', __('Imgs'))->lightbox(['width' => 50, 'height' => 50, 'class' => [ 'thumbnail']]);
        $grid->column('url', __('Url'))->display(function ($url, $column){
            if ($this->type == 2){
//                return $column->video(['videoWidth' => 720, 'videoHeight' => 480]);
            }
            return "";
        });
        $grid->column('is_show', __('Is show'))->switch(CommonStatement::default_switch);;
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Posts::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('posts_cate_id', __('Posts cate id'));
        $show->field('share_num', __('Share num'));
        $show->field('up_num', __('Up num'));
        $show->field('comment_num', __('Comment num'));
        $show->field('goods_id', __('Goods id'));
        $show->field('type', __('Type'));
        $show->field('title', __('Title'));
        $show->field('content', __('Content'));
        $show->field('imgs', __('Imgs'));
        $show->field('url', __('Url'));
        $show->field('is_show', __('Is show'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Posts);

        $form->number('user_id', __('User id'))->readonly();
        $form->number('posts_cate_id', __('Posts cate id'));
        $form->number('share_num', __('Share num'));
        $form->number('up_num', __('Up num'));
        $form->number('comment_num', __('Comment num'));
        $form->number('goods_id', __('Goods id'));
        $form->select('type', __('Type'))->options(CommonStatement::posts_type)->default(1);;
        $form->text('title', __('Title'));
        $form->text('content', __('Content'));
        $form->switch('is_system', __('是否为系统用户发布的帖子'))
            ->states(CommonStatement::default_switch)
            ->default(CommonStatement::default_status);
        $form->multipleImage('imgs', __('Imgs'));
        $form->url('url', __('Url'));
        $form->switch('is_show', __('Is show')) ->states(CommonStatement::default_switch)
            ->default(CommonStatement::default_status);

        return $form;
    }
}
